﻿using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace Module.Common.Common.QiPuHelper
{
    /// <summary>
    /// 着法类
    /// </summary>
    public class StepData : BindableBase
    {
        private int _id;
        public int ID { get { return _id; } set { SetProperty(ref _id, value); } }
        public int QiZiId { get; set; } // 棋子编号
        public int X0 { get; set; } // 移动前位置
        public int Y0 { get; set; }
        public int X1 { get; set; } // 移动后位置
        public int Y1 { get; set; }
        public int DieQz { get; set; } // 移动后杀死的棋子
        public string Nm { get; set; }
        private string _cn;
        public string Cn { get { return _cn; } set { SetProperty(ref _cn, value); } }

        public string UcciStep { get; set; }
        public string Remarks { get; set; } // 备注
        private SolidColorBrush _textColorBrush; // 红方着法显示为橘红色
        public SolidColorBrush TextColorBrush { get { return _textColorBrush; } set { SetProperty(ref _textColorBrush, value); } }
        public StepData()
        {
            ID = 0;
        }
        public void SetStepData(int qiZi, int x0, int y0, int x1, int y1, int dieQz)
        {
            QiZiId = qiZi;
            X0 = x0;
            Y0 = y0;
            X1 = x1;
            Y1 = y1;
            DieQz = dieQz;
            Nm = $"{qiZi:d2} {x0:d} {y0:d} {x1:d} {y1:d} {DieQz:d}";
            Cn = TranslateToCN(qiZi, x0, y0, x1, y1);
            string cols = "abcdefghi";
            string rows = "9876543210";
            UcciStep = string.Concat(cols.AsSpan(X0, 1), rows.AsSpan(Y0, 1), cols.AsSpan(X1, 1), rows.AsSpan(Y1, 1));

            SolidColorBrush solid = new()
            {
                Color = QiZiId is >= 0 and <= 15 ? Colors.Black : Colors.OrangeRed
            };
            TextColorBrush = solid;
        }


        private readonly string[] CnNumber = { "", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        /// <summary>
        /// 转换为中文走棋指令
        /// </summary>
        /// <param name="QiZi"></param>
        /// <param name="x0"></param>
        /// <param name="y0"></param>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <returns></returns>
        public string TranslateToCN(int QiZi, int x0, int y0, int x1, int y1)
        {
            string char1 = GlobalData.QiZiCnName(QiZi);
            string char2 = QiZi is >= 0 and <= 15 ? (x0 + 1).ToString() : CnNumber[9 - x0];
            string char3 = "";
            string char4;
            #region 棋谱翻译为中文
            int m = Math.Abs(y1 - y0);
            // 进退平
            if (y0 == y1)
            {
                char3 = "平";
                char4 = QiZi is >= 0 and <= 15 ? (x1 + 1).ToString() : CnNumber[9 - x1];
            }
            else
            {
                if (QiZi is >= 0 and <= 15)
                {
                    char3 = y1 > y0 ? "进" : "退";
                }
                if (QiZi is >= 16 and <= 31)
                {
                    char3 = y1 > y0 ? "退" : "进";
                }

                char4 = QiZi switch
                {
                    1 or 2 or 3 or 4 or 5 or 6 => (x1 + 1).ToString(),
                    17 or 18 or 19 or 20 or 21 or 22 => CnNumber[9 - x1],
                    // 其他所有可以直走的棋子
                    _ => QiZi is >= 0 and <= 15 ? m.ToString() : CnNumber[m],
                };

            }
            #endregion
            return char1 + char2 + char3 + char4;
        }
        /// <summary>
        /// 执行着法
        /// </summary>
        public void LunchStep()
        {
            if (QiZiId is > -1 and < 32)
            {
                //GlobalValue.QiZiMoveTo(QiZi, X1, Y1, true);
            }
        }
    }
}
